'use strict';

const Controller = require('egg').Controller;

class StufileController extends Controller {
    // 创建控制器——POST——/name
    async create() {
        const { ctx } = this;
        const { filename, file_dir, progress, stu_id } = ctx.request.body;
        const canbedelete = 0;
        const sfile = {
            filename,
            file_dir,
            progress,
            stu_id,
            canbedelete
        }
        const result = await ctx.service.stufile.add(sfile);

        ctx.status = 201;
        ctx.body = result;
    }

    // // 查询获取数据用控制器——GET——/name?keywords=?&&.....
    async index() {
        const { ctx } = this;
        const keywords = ctx.query.keywords ? ctx.query.keywords : '';
        const pageIndex = ctx.query.pageIndex ? +ctx.query.pageIndex : 1;
        const pageSize = ctx.query.pageSize ? +ctx.query.pageSize : 5;
        console.log('keywords', keywords);
        const result = await ctx.service.stufile.findList(keywords, pageIndex, pageSize);
        ctx.status = 200;
        ctx.body = result;
    }
    // // // 根据id获取信息的控制器方法——GET——/name/:id
    async show() {
        const { ctx } = this;
        const id = ctx.params.id;
        const result = await ctx.service.stufile.findById(id);
        ctx.status = 200;
        ctx.body = result;
    }
    // // // 根据id修改对应的数据的控制器方法——PUT——/name/:id
    async update() {
        const { ctx } = this;
        const id = ctx.params.id;
        const {
            canbedelete
        } = ctx.request.body;
        const result = await ctx.service.stufile.modify({
            id,
            canbedelete
        });
        ctx.body = result;
    }
    // // // // 根据id删除对应的数据的控制器方法——DELETE——/name/:id
    async destroy() {
        const { ctx } = this;
        const id = ctx.params.id;
        const result = await ctx.service.stufile.remove(id);
        ctx.status = 200;
        ctx.body = result;
    }

    async findByStuCurId() {
        const { ctx } = this;
        const stu_id = ctx.query.stu_id;
        const keywords = ctx.query.keywords ? ctx.query.keywords : '';
        const pageIndex = ctx.query.pageIndex ? +ctx.query.pageIndex : 1;
        const pageSize = ctx.query.pageSize ? +ctx.query.pageSize : 5;
        console.log('keywords', keywords);
        const result = await ctx.service.stufile.findByStuCurrentId(keywords, pageIndex, pageSize, stu_id);
        ctx.status = 200;
        ctx.body = result;
    }

    async findbyNeedReview() {
        const { ctx } = this;
        console.log('ctx.request.body', ctx.request.body);
        const { stu_id, need } = ctx.request.body;
        const result = await ctx.service.stufile.findbyNeedReviewby(stu_id,need);
        ctx.status = 200;
        ctx.body = result;
        // console.log('ctx.query',ctx.query); 
    }
}

module.exports = StufileController;
